angular.module('app')
    .directive('appZtree', ['$timeout', '$parse', 'uiLoad', 'JQ_CONFIG', function ($timeout, $parse, uiLoad, JQ_CONFIG) {
        return {
        	require: '?ngModel',
            scope: {
                setting: '=appZtree',
                data: '=treeDate'
            },
            restrict: 'A',
            link: function ($scope, element, attrs, ngModel) {
            	element.addClass('ztree');
                uiLoad.load(JQ_CONFIG['zTree']).then(function () {
                    var zTree = {};
                    var setting = {
                        data: {
                            key: {
                                title: "name",
                                name : "name"
                            },
                            simpleData: {
                                enable: true,
                                idKey: "id",
                                pIdKey: "parentId",
                            	rootPId : '0'
                            }
                        },
                        callback: {}
                    };

                    if (attrs.checkBox) {
                        setting.check = {
                            enable: true,
                            chkStyle: "checkbox",
                            chkboxType: {"Y": "p", "N": "s"}
                        }
                    } else {
                    	setting.callback.onClick =function (event, treeId, treeNode, clickFlag) {
                            $scope.$apply(function () {
                                ngModel.$setViewValue(treeNode);
                            });
                        }
                    }
                    if ($scope.setting && angular.isObject($scope.setting)) {
                        setting = $.extend(true, setting, $scope.setting);
                    }
                    zTree.init = function () {
                        var zNodes = $scope.data || [];
                        var ztree = $.fn.zTree.init(element, setting, zNodes);
                        if(attrs.treeObj) {
                        	var ztreeObj = $parse(attrs.treeObj);
                        	setZtreeObj = ztreeObj.assign;
                        	setZtreeObj($scope.$parent, ztree);
                        }
                        
                        // 隐藏节点
                        if(attrs.treeHideNode) {
                        	var tid = $parse(attrs.treeHideNode)($scope.$parent);
                        	function filter(node) {
                        	    return node.id == tid;
                        	}
                        	var node = ztree.getNodesByFilter(filter, true);
                        	ztree.removeNode(node);
                        }
                    }
                    $scope.$watch('data', function () {
                        zTree.init();
                    },true);
                });
            }
        };
    }]);